package com.mygdx.game.Stage;


import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Scenes.ActiveCharacter;
import com.mygdx.game.Scenes.ActiveCharacter_Pope;
import com.mygdx.game.Scenes.Character_A;
import com.mygdx.game.Screens.BeginScreen;
import com.mygdx.game.Screens.PlayScreen;

public class IndexStage extends Stage {
    private float timer;


    // 纹理画布
    private SpriteBatch batch;
    // 纹理
    private Texture texture;
    private Texture master;
    private Texture Group1;
    private Texture Group2;
    private Image GroupImage1;
    private Image GroupImage2;


    // 背景音乐
    private Music music;
    // 音效
    private Sound sound;


    private static final String TAG = MyGdxGame.class.getSimpleName();
    // 按钮 弹起 状态的纹理
    private Texture upTextureStart;
    private Texture upTextureLoad;
    private Texture upTextureOption;
    private Texture upTextureExit;


    // 按钮 按下 状态的纹理
    private Texture downTextureStart;
    private Texture downTextureLoad;
    private Texture downTextureOption;
    private Texture downTextureExit;

    private Texture musicTextureTurnOff;
    private Texture musicTextureTurnOn;

    // 按钮
    private Button buttonStart;
    private Button buttonLoad;
    private Button buttonOption;
    private Button buttonExit;
    private Button buttonMusic;

    private Button.ButtonStyle styleStart ;
    private Button.ButtonStyle styleLoad ;
    private Button.ButtonStyle styleOption;
    private Button.ButtonStyle styleExit ;
    private Button.ButtonStyle styleMusicOn ;
    private Button.ButtonStyle styleMusicOff;



    private Texture textureindex;
    private Image indeximage;
    private Image morgenimage;

    public IndexStage() {
        super();
        init();
    }
    private void init() {
        //背景
//        music = Gdx.audio.newMusic(Gdx.files.internal("music/GG.mp3"));  // music
//        // 背景音乐设置循环播放
//        music.setLooping(true);
//        music.setVolume((float) 0.8);
//        music.play();

        textureindex = new Texture(Gdx.files.internal("start/background.jpg"));
//        indexbackground = new TextureRegion(textureindex, 0, 0, 1000, 1080);
        indeximage = new Image(textureindex);
        indeximage.setSize(MyGdxGame.V_WIDTH, MyGdxGame.V_HEIGHT);
        master = new Texture("start/Morgen.png");
        Group1 = new Texture("start/Group1.png");
        Group2 = new Texture("start/Group2.png");

        morgenimage = new Image(master);
        morgenimage.setPosition(1350,170);
        morgenimage.setSize(500,500);


        /*
         * 按钮的设置
         */
        // 设置日志输出级别
        Gdx.app.setLogLevel(Application.LOG_DEBUG);



        /*
         * 第 1 步: 创建 弹起 和 按下 两种状态的纹理
         */
        upTextureStart = new Texture(Gdx.files.internal("start/start_1.png"));
        downTextureStart = new Texture(Gdx.files.internal("start/start.png"));

        upTextureLoad = new Texture(Gdx.files.internal("start/load_1.png"));
        downTextureLoad = new Texture(Gdx.files.internal("start/load.png"));

        upTextureOption = new Texture(Gdx.files.internal("start/option_1.png"));
        downTextureOption = new Texture(Gdx.files.internal("start/option.png"));

        upTextureExit = new Texture(Gdx.files.internal("start/exit_1.png"));
        downTextureExit = new Texture(Gdx.files.internal("start/exit.png"));

        musicTextureTurnOff = new Texture(Gdx.files.internal("start/musicTurnOff.png"));
        musicTextureTurnOn = new Texture(Gdx.files.internal("start/musicTurnOn.png"));

        /*
         * 第 2 步: 创建 ButtonStyle
         */
        styleStart = new Button.ButtonStyle();
        styleLoad = new Button.ButtonStyle();
        styleOption = new Button.ButtonStyle();
        styleExit = new Button.ButtonStyle();
        styleMusicOn = new Button.ButtonStyle();
        styleMusicOff = new Button.ButtonStyle();

        // 设置 style 的 弹起 和 按下 状态的纹理区域
        styleStart.up = new TextureRegionDrawable(new TextureRegion(upTextureStart));
        styleStart.down = new TextureRegionDrawable(new TextureRegion(downTextureStart));

        styleLoad.up = new TextureRegionDrawable(new TextureRegion(upTextureLoad));
        styleLoad.down = new TextureRegionDrawable(new TextureRegion(downTextureLoad));

        styleOption.up = new TextureRegionDrawable(new TextureRegion(upTextureOption));
        styleOption.down = new TextureRegionDrawable(new TextureRegion(downTextureOption));

        styleExit.up = new TextureRegionDrawable(new TextureRegion(upTextureExit));
        styleExit.down = new TextureRegionDrawable(new TextureRegion(downTextureExit));

        styleMusicOn.up = new TextureRegionDrawable(new TextureRegion(musicTextureTurnOn));
        styleMusicOff.up = new TextureRegionDrawable(new TextureRegion(musicTextureTurnOff));

        /*
         * 第 3 步: 创建 Button
         */
        buttonStart = new Button(styleStart);
        buttonLoad = new Button(styleLoad);
        buttonOption = new Button(styleOption);
        buttonExit = new Button(styleExit);
        buttonMusic = new Button(styleMusicOff);

        GroupImage1 = new Image(Group1);
        GroupImage2 = new Image(Group2);
        GroupImage1.setPosition(400,600);
        GroupImage1.setSize(1200,400);
        GroupImage2.setPosition(400,600);
        GroupImage2.setSize(1200,400);

        // 设置按钮的位置
        buttonStart.setPosition(830, 445);
        buttonLoad.setPosition(830, 328);
        buttonOption.setPosition(830, 211);
        buttonExit.setPosition(830, 94);
        buttonMusic.setPosition(1800,80);

        buttonStart.setSize(316,119);
        buttonLoad.setSize(316,119);
        buttonOption.setSize(316,119);
        buttonExit.setSize(316,119);
        buttonMusic.setSize(70,70);



        this.addActor(indeximage);

        this.addActor(buttonStart);
        this.addActor(buttonLoad);
        this.addActor(buttonOption);
        this.addActor(buttonExit);
        this.addActor(buttonMusic);
        this.addActor(GroupImage2);
        this.addActor(GroupImage1);
        this.addActor(morgenimage);
        Button();


    }
    public void Button(){
        // 给按钮添加点击监听器
        buttonOption.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                PlayScreen.flag = 2;
                Gdx.app.log(TAG, "Game option");
            }
        });
        buttonStart.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                PlayScreen.flag = 1;
                Gdx.app.log(TAG, "Game starts");
            }
        });
        buttonExit.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.log(TAG, "exit");
                Gdx.app.exit();
            }
        });
        buttonMusic.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                if (buttonMusic.getStyle() == styleMusicOff) {
                    BeginScreen.music.pause();
                    buttonMusic.setStyle(styleMusicOn);
                }
                else if(buttonMusic.getStyle() == styleMusicOn) {
                    BeginScreen.music.play();
                    buttonMusic.setStyle(styleMusicOff);
                }
            }
        });

    }
}
